package com.oos.zone.wechat.ma.service.handler;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaMessage;
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
import cn.binarywang.wx.miniapp.message.WxMaXmlOutMessage;
import me.chanjar.weixin.common.session.WxSessionManager;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;

/**
 * 功能描述：
 *
 * @author 拓金辉
 * @version 1.0
 * @date 2021-08-12 17:50:20
 */
public abstract class AbstractWxMaMessageHandler implements WxMaMessageHandler {

    protected final WxMaXmlOutMessage defaultTextMessage = WxMaXmlOutMessage.builder().build();
    public WxMaMessage wxMaMessage;
    public Map<String, Object> context;
    public WxMaService wxMaService;
    public WxSessionManager sessionManager;

    /**
     * 处理消息.
     *
     * @param message        输入消息
     * @param context        上下文
     * @param service        服务类
     * @param sessionManager session管理器
     * @return 输出消息
     */
    @Override
    public WxMaXmlOutMessage handle(WxMaMessage message, Map<String, Object> context, WxMaService service, WxSessionManager sessionManager) {
        context = Optional.ofNullable(context).orElseGet(Collections::emptyMap);
        this.wxMaMessage = message;
        this.context = context;
        this.wxMaService = service;
        this.sessionManager = sessionManager;
        return this.handle0();
    }

    /**
     * 方法描述：真正处理业务的方法
     *
     * @return cn.binarywang.wx.miniapp.message.WxMaXmlOutMessage
     *
     * @author 拓金辉
     * @date 2021-08-13 18:09:09
     */
    public abstract WxMaXmlOutMessage handle0();

}
